package com.itheima.web.admin;

import com.github.pagehelper.PageInfo;
import com.itheima.model.ResponseData.ArticleResponseData;
import com.itheima.model.ResponseData.StaticticsBo;
import com.itheima.model.domain.Article;
import com.itheima.model.domain.Category;
import com.itheima.model.domain.Comment;
import com.itheima.model.domain.User;
import com.itheima.service.IArticleService;
import com.itheima.service.ISiteService;
import com.itheima.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Classname AdminController
 * @Description 后台管理模块
 * @Date 2019-3-14 10:39
 * @Created by CrazyStone
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
    private static final Logger logger = LoggerFactory.getLogger(AdminController.class);

    @Autowired
    private ISiteService siteServiceImpl;
    @Autowired
    private IArticleService articleServiceImpl;

    @Autowired
    private IUserService userServiceImpl;


    // 管理中心起始页
    @GetMapping(value = {"", "/index"})
    public String index(HttpServletRequest request) {
        // 获取最新的5篇博客、评论以及统计数据
        List<Article> articles = siteServiceImpl.recentArticles(5);
        List<Comment> comments = siteServiceImpl.recentComments(5);
        StaticticsBo staticticsBo = siteServiceImpl.getStatistics();
        // 向Request域中存储数据
        request.setAttribute("comments", comments);
        request.setAttribute("articles", articles);
        request.setAttribute("statistics", staticticsBo);
        return "back/index";
    }

    // 向文章发表页面跳转
    @GetMapping(value = "/article/toEditPage")
    public String newArticle() {
        return "back/article_edit";
    }

    // 发表文章
    @PostMapping(value = "/article/publish")
    @ResponseBody
    public ArticleResponseData publishArticle(Article article) {
        if (StringUtils.isBlank(article.getCategories())) {
            article.setCategories("默认分类");
        }
        try {
            articleServiceImpl.publish(article);
            logger.info("文章发布成功");
            return ArticleResponseData.ok();
        } catch (Exception e) {
            logger.error("文章发布失败，错误信息: " + e.getMessage());
            return ArticleResponseData.fail();
        }
    }

    // 跳转到后台文章列表页面
    @GetMapping(value = "/article")
    public String index(@RequestParam(value = "page", defaultValue = "1") int page,
                        @RequestParam(value = "count", defaultValue = "10") int count,
                        HttpServletRequest request) {
        PageInfo<Article> pageInfo = articleServiceImpl.selectArticleWithPage(page, count);
        request.setAttribute("articles", pageInfo);
        return "back/article_list";
    }

    // 向文章修改页面跳转
    @GetMapping(value = "/article/{id}")
    public String editArticle(@PathVariable("id") String id, HttpServletRequest request) {
        Article article = articleServiceImpl.selectArticleWithId(Integer.parseInt(id));
        request.setAttribute("contents", article);
        request.setAttribute("categories", article.getCategories());
        System.out.println(article);
        return "back/article_edit";
    }

    // 文章修改处理
    @PostMapping(value = "/article/modify")
    @ResponseBody
    public ArticleResponseData modifyArticle(Article article) {
        try {
            articleServiceImpl.updateArticleWithId(article);
            logger.info("文章更新成功");
            return ArticleResponseData.ok();
        } catch (Exception e) {
            logger.error("文章更新失败，错误信息: " + e.getMessage());
            return ArticleResponseData.fail();
        }
    }

    //  文章预览
    @GetMapping(value = "/articlepreview/{id}")
    public String previewArticle(@PathVariable("id") String id, HttpServletRequest request) {
        Article article = articleServiceImpl.selectArticleWithId(Integer.parseInt(id));
        request.setAttribute("article", article);
        System.out.println(article);
        return "back/preview";
    }

    // 文章删除
    @PostMapping(value = "/article/delete")
    @ResponseBody
    public ArticleResponseData delete(@RequestParam int id) {
        try {
            articleServiceImpl.deleteArticleWithId(id);
            logger.info("文章删除成功");
            return ArticleResponseData.ok();
        } catch (Exception e) {
            logger.error("文章删除失败，错误信息: " + e.getMessage());
            return ArticleResponseData.fail();
        }
    }

    //打开评论
    @PutMapping("/opencom")
    @ResponseBody
    public ArticleResponseData opencom(@RequestParam int id) {
        try {
            articleServiceImpl.openAllowCommentByID(id);
            logger.info("开启成功");
            return ArticleResponseData.ok();

        } catch (Exception e) {
            logger.error("开启失败，错误信息: " + e.getMessage());
            return ArticleResponseData.fail();

        }
    }

    //关闭评论
    @PutMapping("/closecom")
    @ResponseBody
    public ArticleResponseData closecom(@RequestParam int id) {
        try {
            articleServiceImpl.closeAllowCommentByID(id);
            logger.info("关闭成功");
            return ArticleResponseData.ok();

        } catch (Exception e) {
            logger.error("关闭失败，错误信息: " + e.getMessage());
            return ArticleResponseData.ok();
        }
    }
    // 评论编辑
    @GetMapping(value = "/comment")
    public String comment(@RequestParam(value = "page", defaultValue = "1") int page,
                          @RequestParam(value = "count", defaultValue = "10") int count,
                          HttpServletRequest request) {
        PageInfo<Article> pageInfo = articleServiceImpl.selectArticleWithPage(page, count);
        request.removeAttribute("articles");
        request.setAttribute("articles", pageInfo);
        return "/back/comment_edit";
    }
    //分类管理
    @GetMapping(value = "/category")
    public String category(@RequestParam(value = "page", defaultValue = "1") int page,
                           @RequestParam(value = "count", defaultValue = "10") int count,
                           HttpServletRequest request) {
        PageInfo<Article> pageInfo = articleServiceImpl.selectArticleWithPage(page, count);
        List<Category> categoryList = articleServiceImpl.getcategoryArticle(page, count);
        System.out.println(categoryList);
        request.setAttribute("categoryList",categoryList);
        request.removeAttribute("articles");
        request.setAttribute("articles", pageInfo);
        return "back/category";
    }
    //跳转分类编辑页面
    @GetMapping(value = "/category/{categories}")
    public String editcategory(@PathVariable("categories") String categories, HttpServletRequest request) {
        request.setAttribute("categories", categories);
        System.out.println(categories);
        return "back/preview";
    }

    //分类相关文章
    @GetMapping("/category/see/{categories}")
    public String showArticle(@PathVariable("categories") String categories,HttpServletRequest request){
        List<Article> articleList = articleServiceImpl.selectArticleWithCategories(categories);
        request.setAttribute("articles", articleList);
        System.out.println(articleList);
        return "/back/category_edit";
    }
    //分类删除
    @DeleteMapping("admin/category/delete")
    public ArticleResponseData deleteCategory(@RequestParam String categories) {
        try {
            articleServiceImpl.deleteArticleWithcategory(categories);
            logger.info("文章删除成功");
            return ArticleResponseData.ok();
        } catch (Exception e) {
            logger.error("文章删除失败，错误信息: " + e.getMessage());
            return ArticleResponseData.fail();
        }
    }
    //分类查询
    @GetMapping("/category/search")
    public String search(@RequestParam(value = "keywords", required = false) String keywords, HttpServletRequest request) {
        System.out.println(keywords);
        List<Category> categoryList =null;
        if (keywords != null && !keywords.isEmpty()) {
            categoryList = articleServiceImpl.searchbykeyword(keywords);
        }else{
           categoryList = articleServiceImpl.getcategoryArticle(1, 10);
        }
        request.setAttribute("categoryList", categoryList);
        System.out.println(categoryList);
        return "back/category";
    }

    // 用户管理
    @GetMapping(value = "/setting")
    public String setting(@RequestParam(value = "page", defaultValue = "1") int page,
                          @RequestParam(value = "count", defaultValue = "10") int count,
                          HttpServletRequest request) {
        PageInfo<User> pageInfo = userServiceImpl.getAllUsers(page, count);
        request.setAttribute("users", pageInfo);
        System.out.println(request.getAttribute("users"));
        return "back/setting";
    }


    // 用户删除
    @PostMapping("/user/delete")
    @ResponseBody
    public void deleteUser(@RequestParam int id) {
        try {
            userServiceImpl.deleteUserById(id);
            logger.info("用户删除成功");
        } catch (Exception e) {
            logger.error("文章删除失败，错误信息: " + e.getMessage());
        }
    }

    //用户编辑
    // 向文章修改页面跳转
    @GetMapping(value = "/user/{id}")
    public String editUser(@PathVariable("id") String id, HttpServletRequest request) {
        User user = userServiceImpl.selectUserById(Integer.parseInt(id));
        request.setAttribute("contents", user);
//        request.setAttribute("categories", article.getCategories());
        return "back/article_edit";
    }



}

